package com.info.zhiduoduo.pay.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayDataBillBalanceQueryResponse;
import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.exception.SmartException;
import com.info.zhiduoduo.pay.formbean.zhaoshangbankpay.AlipayAccountQueryFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangbankpay.AlipayTransferFormBean;
import com.info.zhiduoduo.pay.formbean.zhaoshangbankpay.AlipayTransferQueryFormBean;
import com.info.zhiduoduo.pay.sdk.alipaysdk.AliPayBaseApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 安全发（人资ISV版）文档: https://opendocs.alipay.com/open/01kwnq
 */
@Service
@Slf4j
public class AliPayApiService {
    @Autowired
    AliPayBaseApiService aliPayBaseApiService;

    /**
     * 调用免密单笔转账接口
     *
     * @param formBean
     * @throws AlipayApiException
     */
    public SmartResult transfer(AlipayTransferFormBean formBean) {
        try {
            AlipayFundTransUniTransferResponse transfer = aliPayBaseApiService.transfer(formBean);
            if (null != transfer) {
                return SmartResult.success(transfer);
            } else {
                return SmartResult.fail(StatusMsg.StatusMsg_446);
            }
        } catch (SmartException e) {
            return SmartResult.fail(StatusMsg.StatusMsg_446);
        }
    }

    /**
     * 调用免密单笔转账接口
     *
     * @param formBean
     * @throws AlipayApiException
     */
    public SmartResult transQuery(AlipayTransferQueryFormBean formBean) {
        try {
            AlipayFundTransCommonQueryResponse transfer = aliPayBaseApiService.transferQuery(formBean);
            if (null != transfer) {
                return SmartResult.success(transfer);
            } else {
                return SmartResult.fail(StatusMsg.StatusMsg_446);
            }
        } catch (SmartException e) {
            return SmartResult.fail(StatusMsg.StatusMsg_446);
        }

    }

    /**
     * 支付宝资金账户资产查询接口
     *
     * @param formBean
     * @throws AlipayApiException
     */
    public SmartResult accountQuery(AlipayAccountQueryFormBean formBean) {
        try {
            AlipayDataBillBalanceQueryResponse query = aliPayBaseApiService.accountQuery(formBean);
            if (null != query) {
                return SmartResult.success(query);
            } else {
                return SmartResult.fail(StatusMsg.StatusMsg_486);
            }
        } catch (SmartException e) {
            return SmartResult.fail(StatusMsg.StatusMsg_486);
        }

    }

}
